/*
 * 								SYMBIOS
 *
 * 	gesture_rec.cpp
 *  Created on: Jun 1, 2009
 *  Author: andrei
 */

#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include "gesture_rec.h"
#include "parameters.h"

IplImage*     g_gray    = NULL;
int           g_thresh = 100;
CvMemStorage* g_storage = NULL;

//////////////////////////////////
// traceContour()
//

int traceContour(IplImage* source, CvBox2D32f handBox)
{
//INIT : reset ROI
  CvSeq* contours = 0;			// Le contours = listes chainées de CvPoint
  int nContours =0;
  cvResetImageROI(source);

//CREATION DUN ESPACE MEMOIRE POUR LE CONTOUR
  if( g_storage==NULL ) {
    g_storage = cvCreateMemStorage(0);
  }
  else {
    cvClearMemStorage( g_storage );
  }

//CREATION DU CONTOUR
  nContours = cvFindContours( source, g_storage, &contours,sizeof(CvContour),CV_RETR_EXTERNAL ,CV_CHAIN_APPROX_SIMPLE);			//CV_RETR_EXTERNAl : for external contours only

//DESSINER LE CONTOUR DANS L'IMAGE SOURCE
  cvZero( source );								//on remet l'image a zero
  if( contours )
    {
	  cvDrawContours( source, contours, cvScalarAll(255), cvScalarAll(255),100);
	  return nContours;		//the number of contour found
    }
  return 0;					//if  failure
}

//////////////////////////////////
// traceMinRect()
//

int traceMinRect(IplImage* contour, IplImage* source, CvPoint2D32f center)
{
//INIT
	if (center.x ==0 || center.y==0) return 0;				//Check that hand is on screen
	IplImage* contourCopy = cvCloneImage(contour);			//Work on a copy
	cvResetImageROI(contourCopy);							//The hole image is now considered selected

//PREMIER RECTANGLE de taille SYMB_REC_SCALEX x SYMB_REC_SCALEY
	float SCALEX=SYMB_REC_SCALEX;
	float SCALEY=SYMB_REC_SCALEY;
	int x_rect1 = (int)center.x-(int)SCALEX;
	int y_rect1 = (int)center.y-(int)SCALEY;
	int x_rect2 = (int)center.x+(int)SCALEX;
	int y_rect2 = (int)center.y+(int)SCALEY;
	cvSetImageROI(contourCopy, cvRect((int)center.x,(int)center.y,(int)SCALEX*2,(int)SCALEY*2));
	CvScalar value = cvAvg(contourCopy);					 //To check dominance of white or black

//CROISSANCE GRADUELLE DU RECTANGLE
//condition d'arret : trop de noir
int loop=0;
while ((value.val[0] > SYMB_REC_SEUIL ||  (x_rect1==1 && x_rect2==639 && y_rect1 == 1 && y_rect2 == 479)) && loop < SYMB_REC_LOOP) {
			loop++;
			SCALEX =  SCALEX*SYMB_REC_STEPX;
			SCALEY =  SCALEY* SYMB_REC_STEPY;
			x_rect1 = (int)center.x-(int)SCALEX;
			y_rect1 = (int)center.y-(int)SCALEY;
			x_rect2 = (int)center.x+(int)SCALEX;
			y_rect2 = (int)center.y+(int)SCALEY;
			if (x_rect1<2) x_rect1=1;
			if (x_rect2>638) x_rect2=639;
			if (y_rect1<2) y_rect1=1;
			if (y_rect2>478) y_rect2=479;
			cvSetImageROI(contourCopy, cvRect((int)center.x,(int)center.y,x_rect2-x_rect1,y_rect2-y_rect1));
			value = cvAvg(contourCopy);
}

	//SHOW RECTANGLE
	cvRectangle(source,cvPoint(x_rect1,y_rect1),cvPoint(x_rect2,y_rect2), CV_RGB(0,255,0), 1, 8, 0 );
	//RETURN SIZE FOR RECOGNITION of SELECT/RELEASE
	int size = y_rect1-y_rect2;
	return size;

}


void closeGestureRec()
{
	cvClearMemStorage(g_storage);
}
